import socket from '@ohos.net.socket';
import httptool from "../../common/js/httptool.js"

export default {
    data: {
        tcp:null,
        todaygross:100,
        autoweight:0,
        hmweight:10,
        cm:0

    },
    onInit() {

    },
    onShow() {
        this.onSocKets();
    },
    setHmweight(e){
        this.hmweight=e._detail;
    },
    setAutoweight(e){
        this.autoweight =this.autoweight+ e._detail;
    },
    onSocKets(){
        this.tcp = socket.constructTCPSocketInstance();
        this.$app.$def.datas.tcp=this.tcp;
        this.tcp.bind({address: this.$app.$def.datas.tcpipself, port: this.$app.$def.datas.portself, family: 1}, err => {
            if (err) {
                console.log('bind fail');
                return;
            }
            console.log('bind success');
        });
        this.tcp.connect({ address: {address: this.$app.$def.datas.tcpip, port: this.$app.$def.datas.port, family: 1} , timeout: 6000}, err => {
            if (err) {
                console.log('connect fail');
                return;
            }
            console.log('connect success');
        })
        this.tcp.on('message', value => {
            let buf = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(value.message)));
            this.cm = ((1-(buf.cm-3)/(9.4-3))*100).toFixed(1);
            httptool.http_post(this.$app.$def.datas.serverurl+"/surplusGrainRecord_add",{    name: "weight", weight: this.cm});

        });


    },
}
